package com.zz.cc.business.app;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zz.cc.business.R;
import com.zz.cc.business.data.ServiceInfo;
import com.zz.common.app.BaseActivity;
import com.zz.common.widget.ZListView;

public class ServiceListActivity extends BaseActivity {
	
	private ZListView mListView;
	private ServiceAdapter mAdapter;
	private final List<ServiceInfo> mServices = new ArrayList<ServiceInfo>();
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_service_list);
		initUI();
		fillView();
		refresh();
	}
	
	private void initUI() {
		mListView = (ZListView) findViewById(R.id.service_list_view);
	}
	
	private void fillView() {
		mAdapter = new ServiceAdapter();
		mListView.setAdapter(mAdapter);
	}
	
	private void refresh() {
		mServices.clear();
		mServices.addAll(ServiceInfo.generateServiceInfoList());
		mAdapter.notifyDataSetChanged();
	}
	
	private static class ViewHolder {
		public TextView mTvSummary;
	}
	
	private class ServiceAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mServices.size();
		}

		@Override
		public Object getItem(int position) {
			return mServices.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = LayoutInflater.from(ServiceListActivity.this).inflate(R.layout.list_item_service, null);
				holder = new ViewHolder();
				holder.mTvSummary = (TextView) convertView.findViewById(R.id.service_summary);
				
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ServiceInfo s = mServices.get(position);
			String summary = String.format(getString(R.string.service_summary)
					, s.mPhoneNum, s.mCarNum, s.mServiceCount);
			holder.mTvSummary.setText(summary);
			return convertView;
		}
		
	}

}
